import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hotfil_pub_web/pages/home/widgets/core_data.dart';
import 'package:hotfil_pub_web/pages/home/widgets/income_card.dart';
import 'package:hotfil_pub_web/pages/home/widgets/menu_bar.dart';
import 'package:hotfil_pub_web/pages/home/widgets/notice.dart';
import 'package:hotfil_pub_web/pages/home/widgets/text_form_bg_field.dart';
import 'package:hotfil_pub_web/service/home_data.dart';

import 'package:hotfil_pub_web/widgets/my_sliver.dart';

import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class MyHomePage extends StatefulWidget {
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  HomeData homeData = HomeData();
  final GlobalKey _refresherKey = GlobalKey();
  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);
  void initData() async {
    var res = await getHomePageData(showLoading: true);

    setState(() {
      homeData = res;
    });
  }

  @override
  void initState() {
    super.initState();
    initData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        title: const Text(
          '首页',
        ),
      ),
      body: SmartRefresher(
        key: _refresherKey,
        controller: _refreshController,
        onRefresh: () {
          initData();
          _refreshController.refreshCompleted();
        },
        enablePullDown: true,
        child: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                color: Colors.white,
                child: Column(
                  children: [
                    Padding(
                      padding: EdgeInsets.only(left: 20, right: 20),
                      child: HomeIncomeCard(
                        allIncome: homeData.allIncome,
                        availableBalance: homeData.availableBalance,
                        curSavePower: homeData.curSavePower,
                      ),
                    ),
                    const SizedBox(
                      height: 17,
                    ),
                    HomeMenuBar(),
                    const SizedBox(
                      height: 17,
                    ),
                    Padding(
                      padding: const EdgeInsets.only(
                          left: 20, right: 20, bottom: 12),
                      child: HomeNotice(),
                    )
                  ],
                ),
              ),
              MySLiver(),
              HomeCoreData(),
              MySLiver(),
              ElevatedButton(
                  onPressed: () async {
                    // 展示消息
                    // EasyLoading.showToast(
                    //   'Toast',
                    // );
                    // await EasyLoading.show(
                    //   status: '加载中...',
                    //   maskType: EasyLoadingMaskType.black,
                    // );
                    initData();
                  },
                  child: Text('initData')),
              HomeCoreData(),
              MySLiver(),
              HomeCoreData(),
              HomeCoreData(),
              MySLiver(),
              HomeCoreData(),
              BgInput(),
              const SelectableText('data'),
              HomeCoreData(),
            ],
          ),
        ),
      ),
    );
  }
}
